﻿// ***********************************************************
// 管理员账号配置类
// Creator:yehui  Date:2013-02-19
// ***********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using VipSys.SystemEntity;
using VipSys.Common.Library;

namespace VipSys.DataLayer
{
    
    public class AccountAccess
    {
        public static readonly AccountAccess Instance = new AccountAccess();
        private XMLBase m_XmlBase;
        public AccountAccess()
        {
            m_XmlBase = new XMLBase(SystemParam.Instance.WorkPath + "\\SystemConfig.xml");
        }
        public short GetAccounts(ref List<SysAccount> lstAccounts)
        {
            if (lstAccounts == null)
                lstAccounts = new List<SysAccount>();
            lstAccounts.Clear();
            XmlNodeList xmlNodeList = this.m_XmlBase.SelectNodeList(SystemData.XmlConfigItem.Account);
            foreach (XmlNode node in xmlNodeList)
            {
                SysAccount account = new SysAccount();
                account.Name = this.m_XmlBase.ReadXmlNode(node, SystemData.XmlAccount.NAME);
                account.Password = this.m_XmlBase.ReadXmlNode(node, SystemData.XmlAccount.PASSWORD);
                lstAccounts.Add(account);
            }
            return SystemData.ReturnValue.OK;
        }
        public short Edit(SysAccount account)
        {
            XmlNodeList xmlNodeList = this.m_XmlBase.SelectNodeList(SystemData.XmlConfigItem.Account+ "/" + SystemData.XmlAccount.NAME);
            foreach (XmlNode node in xmlNodeList)
            {
                if (node.InnerText == account.Name)
                    this.m_XmlBase.DeleteNode(node.ParentNode);
            }
            XmlElement Node = this.m_XmlBase.XmlDoc.CreateElement("account");
            XmlElement name = this.m_XmlBase.XmlDoc.CreateElement("name");
            name.InnerText = account.Name;
            XmlElement password = this.m_XmlBase.XmlDoc.CreateElement("password");
            password.InnerText = account.Password;
            Node.AppendChild(name);
            Node.AppendChild(password);
            this.m_XmlBase.AddNode(Node, SystemData.XmlConfigItem.Account);
            return SystemData.ReturnValue.OK;
        }

        public short Delete(SysAccount account)
        {
            XmlNodeList xmlNodeList = this.m_XmlBase.SelectNodeList(SystemData.XmlConfigItem.Account+ "/" + SystemData.XmlAccount.NAME);
            foreach (XmlNode node in xmlNodeList)
            {
                if (node.InnerText == account.Name)
                    this.m_XmlBase.DeleteNode(node.ParentNode);
            }
            this.m_XmlBase.SaveXmlDocument();
            return SystemData.ReturnValue.OK;
        }
    }

  
}


